package com.yanqu.road.dao.impl.activity.zhenhuan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.GameZhenHuanPromotionData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.GameZhenHuanFinishPartyData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GameZhenHuanFinishPartyDaoImpl extends TempDataObjectDao<GameZhenHuanFinishPartyData> {
    public static String getGameZhenHuanFinishPartyDataSql = "select * from `t_u_zhenhuan_finish_party` where `activityId`=? and `partyId`=?";

    public GameZhenHuanFinishPartyData getGameZhenHuanFinishPartyData(int activityId, long partyId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGameZhenHuanFinishPartyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, partyId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getAllGameZhenHuanFinishPartyDataSql = "select * from `t_u_zhenhuan_finish_party` where `activityId`=?";

    public Map<Long, GameZhenHuanFinishPartyData> getGameZhenHuanFinishPartyDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, GameZhenHuanFinishPartyData> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getAllGameZhenHuanFinishPartyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GameZhenHuanFinishPartyData data = parse(rs);
                dataMap.put(data.getPartyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateGameZhenHuanFinishPartyDataSql = "update `t_u_zhenhuan_finish_party` set partyLevel=?,finishTime=?,openUserId=?,openServerId=?,userName=?,userDecoration=?,joinUserList=? where activityId=? and partyId=?";

    @Override
    protected boolean updateData(GameZhenHuanFinishPartyData gameZhenHuanFinishPartyData) {
        gameZhenHuanFinishPartyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanFinishPartyData.getPartyLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getOpenServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, gameZhenHuanFinishPartyData.getUserName()));
        para.put(i++, new DbParameter(Types.VARCHAR, gameZhenHuanFinishPartyData.getUserDecoration()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gameZhenHuanFinishPartyData.getJoinUserList())));
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanFinishPartyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getPartyId()));
        if (getDb().executeNoneQuery(updateGameZhenHuanFinishPartyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGameZhenHuanFinishPartyDataSql = "insert into `t_u_zhenhuan_finish_party`(activityId,partyId,partyLevel,finishTime,openUserId,openServerId,userName,userDecoration,joinUserList) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(GameZhenHuanFinishPartyData gameZhenHuanFinishPartyData) {
        gameZhenHuanFinishPartyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanFinishPartyData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getPartyId()));
        para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanFinishPartyData.getPartyLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getFinishTime()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getOpenUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getOpenServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, gameZhenHuanFinishPartyData.getUserName()));
        para.put(i++, new DbParameter(Types.VARCHAR, gameZhenHuanFinishPartyData.getUserDecoration()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gameZhenHuanFinishPartyData.getJoinUserList())));
        if (getDb().executeNoneQuery(insertGameZhenHuanFinishPartyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GameZhenHuanFinishPartyData parseData(ResultSet rs) throws SQLException {
        GameZhenHuanFinishPartyData gameZhenHuanFinishPartyData = new GameZhenHuanFinishPartyData();
        gameZhenHuanFinishPartyData.setActivityId(rs.getInt("activityId"));
        gameZhenHuanFinishPartyData.setPartyId(rs.getLong("partyId"));
        gameZhenHuanFinishPartyData.setPartyLevel(rs.getInt("partyLevel"));
        gameZhenHuanFinishPartyData.setFinishTime(rs.getLong("finishTime"));
        gameZhenHuanFinishPartyData.setOpenUserId(rs.getLong("openUserId"));
        gameZhenHuanFinishPartyData.setOpenServerId(rs.getLong("openServerId"));
        gameZhenHuanFinishPartyData.setUserName(rs.getString("userName"));
        gameZhenHuanFinishPartyData.setUserDecoration(rs.getString("userDecoration"));
        gameZhenHuanFinishPartyData.setJoinUserList(JSON.parseObject(rs.getString("joinUserList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.zhenhuan.secondProcess.ZhenHuanPartyUserSimpleInfo>>() {
        }));

        return gameZhenHuanFinishPartyData;
    }

    public boolean addBatch(List<GameZhenHuanFinishPartyData> saveList) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (GameZhenHuanFinishPartyData gameZhenHuanFinishPartyData : saveList) {
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
            para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanFinishPartyData.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getPartyId()));
            para.put(i++, new DbParameter(Types.INTEGER, gameZhenHuanFinishPartyData.getPartyLevel()));
            para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getFinishTime()));
            para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getOpenUserId()));
            para.put(i++, new DbParameter(Types.BIGINT, gameZhenHuanFinishPartyData.getOpenServerId()));
            para.put(i++, new DbParameter(Types.VARCHAR, gameZhenHuanFinishPartyData.getUserName()));
            para.put(i++, new DbParameter(Types.VARCHAR, gameZhenHuanFinishPartyData.getUserDecoration()));
            para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(gameZhenHuanFinishPartyData.getJoinUserList())));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(insertGameZhenHuanFinishPartyDataSql, mapList) > -1) {
            return true;
        }
        return false;
    }
}
